package test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Hashtable;

public class Evaluator {
 
	
	public static Hashtable<String, String> lireQrel(InputStream ips2) throws NumberFormatException, IOException{
		Hashtable<String, String> qrel = new Hashtable<String, String>();
		InputStreamReader ipsr2=new InputStreamReader(ips2);
		BufferedReader br2=new BufferedReader(ipsr2);
		String ligne2;
		String texte="";
		

		while ((ligne2=br2.readLine())!=null){
			
			String[] nomQrel = ligne2.split("\\t");
		
			if(nomQrel[1].equals("0,5") || nomQrel[1].equals("1")) {
			qrel.put("corpus/"+nomQrel[0], nomQrel[1]);
			}
			//System.out.println(""+nomQrel[0]);
			//System.out.println(""+req[0]);
//			if (req[0].contains(nomQrel[0])){
//				resultat=nomQrel[1];
//				System.out.println("fichier Qrel: "+ligne2);
//				System.out.println("resultat: "+resultat);
//			//}
//			texte+=ligne2+"\n";
		}
		
		return qrel;
	}
	
	public static void main(String argv[])  {
		Hashtable<String, String> qrel;
		String chaine="";
		
		String fichierSortie ="src/fichSortie1.txt";
		String fichierQrel ="qrels/qrelQ1.txt";
		

		
		//lecture fichier sortie
		try{
			InputStream ips=new FileInputStream(fichierSortie); 
			InputStreamReader ipsr=new InputStreamReader(ips);
			BufferedReader br=new BufferedReader(ipsr);
			String ligne;
			
			InputStream ips2=new FileInputStream(fichierQrel); 
			float resultat=0;
			float precision=0;
			
			qrel = lireQrel(ips2);
			
			for (int i=0; i<30; i++){
				ligne = br.readLine();
				if (ligne != null){		
					//System.out.println("fichier Sortie: "+ligne);
					String[] req = null;
					req = ligne.split("\t");
					//qrel = LireQrel(ips2);
					
					
					
					//System.out.println("Qrel: "+qrel);
					//System.out.println("keys: "+qrel.keySet());
					//System.out.println("req: "+req[0]);
					//System.out.println(""+req[0]);
					//if (qrel.containsKey(req[0])){
					//System.out.println("requete " +req[0]);
					if (qrel.containsKey(req[0])){
						resultat++;
					}
					
					if (i==4)
					{
						//System.out.println("test: "+resultat);
						precision = resultat/(i+1);
						System.out.println("precision a 5: "+precision);
					}	
					
					if (i==9)
					{
						precision = resultat/(i+1);
						System.out.println("precision a 10: "+precision);
					}	
					
					if (i==24)
					{
						precision = resultat/(i+1);
						System.out.println("precision a 25: "+precision);
					}	
					
				}
				
				//System.out.println("i: "+i);
				chaine+=ligne+"\n";
			}
			//rappel = nbDocPert/nbDocTotal;
			//precision = rappel/5;
			/*System.out.println("Nombre total de documents: "+nbDocTotal);
			System.out.println("Nombre de documents pertinents: "+nbDocPert);
			System.out.println("Rappel: "+rappel);
			System.out.println("precision: "+precision);*/
			br.close(); 
			
		}		
		catch (Exception e){
			System.out.println(e.toString());
		}
	}

}
